package com.dky.satell.scheduled;

import com.dky.satell.entity.OfflineFileMeta;
import com.dky.satell.service.OfflineFileMetaService;
import com.dky.satell.util.ServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName FileLoad
 * @Description
 * @Author fangyuan
 * @Date 2021/5/30 9:49
 * @Version 1.0
 **/
@Slf4j
@Component
public class FileLoad implements ApplicationRunner {

    /**
     * 文件下载路径
     */
    @Value("${file.downloadPath}")
    private String downloadPath;

    /**
     * 下载加载路径
     */
    @Value("${file.loadPath}")
    private String loadPath;

    @Resource
    private OfflineFileMetaService offlineFileMetaService;

    /**
     * 初始化项目时加载每个业务目录的fileSchema.meta文件
     */
    @Override
    public void run(ApplicationArguments args) {
        File root = new File(downloadPath);
        if(!root.isDirectory()) {
            return;
        }
        String[] fileList = root.list();
        for(String fileName : fileList) {
            if(fileName.endsWith("fileSchema.meta")) {
                String[] metaSplit = fileName.split("_");
                if(!(metaSplit.length == 3)) {
                    continue;
                }
                String directory = metaSplit[1].replaceAll("-", "/");
                File metaFile = new File(loadPath + directory + File.separator + "fileSchema.meta");
                if(!metaFile.exists()) {
                    File file = new File(downloadPath + fileName);
                    try {
                        FileUtils.copyFile(file, metaFile);
                        log.info("目录[{}]描述文件[{}]加载成功", directory, "fileSchema.meta");
                    } catch (IOException e) {
                        log.error("目录[{}]描述文件[{}]加载失败", directory, "fileSchema.meta");
                    }
                }
            }
        }
    }

    /**
     * 定时加载
     */
//    @Scheduled(cron = "*/10 * * * * ?")
    public void scheduled() {
        List<OfflineFileMeta> offlineFileList = offlineFileMetaService.getAllToBeLoaded();
        if(CollectionUtils.isEmpty(offlineFileList)) {
            return;
        }
        for(OfflineFileMeta offlineFile : offlineFileList) {
            File downloadFile = new File(downloadPath + offlineFile.getProxyFileName());
            File downloadMeta = new File(downloadPath + ServiceUtil.buildMetaName(offlineFile.getProxyFileName()));
            if(!downloadFile.exists()) {
                continue;
            }
            File loadFile = new File(loadPath + offlineFile.getFileDirectory() + File.separator + offlineFile.getFileName());
            File loadMeta = new File(loadPath + offlineFile.getFileDirectory() + File.separator + ServiceUtil.buildMetaName(offlineFile.getFileName()));
            //构建加载目录
            if(!loadFile.getParentFile().exists()) {
                loadFile.getParentFile().mkdirs();
            }
            try {
                FileUtils.copyFile(downloadFile,loadFile);
                if(downloadMeta.exists()) {
                    FileUtils.copyFile(downloadMeta,loadMeta);
                }
                log.info("文件[{}]加载完成", offlineFile.getFileName());
            } catch (IOException e) {
                log.error("文件[{}]加载失败", offlineFile.getFileName());
                continue;
            }
            offlineFile.setStatus(3);
        }
        offlineFileMetaService.saveOrUpdateBatch(offlineFileList);
    }

}
